package GUI;

import ModelLayer.*;
import CtrlLayer.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SalesGUI extends JFrame implements ActionListener{
    private Container container;
    private JPanel panelSales;
    private JPanel panelMakeSale;
    private JPanel panelShowSale;
    private JPanel panelShowSale2;
    private JPanel panelDeleteSale;
    private JPanel panelDeleteSale2;
    JLabel labelProduct;
    JLabel errorMessage;
    JTextField textBarcode;
    JTextField textQuantity;
    private int count;
    private int sessionId;
    private int saleId;
    private  SaleController saleController;

    public SalesGUI(int sessionId){
        this.saleId = 0;
        this.sessionId = sessionId;
        saleController = new SaleController();
        this.setTitle("Vestbjerg company system");
        this.setLocation(300,50);
        this.setVisible(true);
        this.setSize(400, 470);
       // this.setResizable(false);
        panelSales = new JPanel(new GridBagLayout());
        panelMakeSale = new JPanel(new GridBagLayout());
        panelShowSale = new JPanel(new GridBagLayout());
        panelShowSale2 = new JPanel(new GridBagLayout());
        panelDeleteSale = new JPanel(new GridBagLayout());
        panelDeleteSale2 = new JPanel(new GridBagLayout());
        count = 0;
        menuSales();

    }

    public void actionPerformed(ActionEvent e){
    }

    public void makeMenuSales(){
        GridBagConstraints gbc = new GridBagConstraints();

        JButton addSale = new JButton("Make a Sale");
        addSale.setPreferredSize(new Dimension(150, 25));
        addSale.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    makeSale();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelSales.add(addSale, gbc);

        JButton showSales = new JButton("Show a Sale");
        showSales.setPreferredSize(new Dimension(150, 25));
        showSales.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){

                    container = getContentPane();
                    container.removeAll();
                    findSale();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelSales.add(showSales, gbc);

        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2){
            JButton deleteSale = new JButton("Delete a Sale");
            deleteSale.setPreferredSize(new Dimension(150, 25));
            deleteSale.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        container = getContentPane();
                        container.removeAll();
                        deleteSaleFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.insets = new Insets(5,5,5,5);
            panelSales.add(deleteSale, gbc);
        }

        JButton back = new JButton("Close");
        back.setPreferredSize(new Dimension(150, 25));
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    dispose();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelSales.add(back, gbc);
    }

    public void menuSales(){
        makeMenuSales();
        this.getContentPane().add(panelSales, BorderLayout.NORTH);
    }

    private void menuMakeSale(){
     

        JTextField textCustomerId = new JTextField();
        JLabel labelCustomerId;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JButton buttonAdd;
        JButton buttonRemove;
        JButton buttonSubmit;
        JButton buttonBack;
        JButton buttonClear;
        GridBagConstraints gbc = new GridBagConstraints();

        labelCustomerId = new JLabel("Customer ID:", JLabel.RIGHT);
        labelCustomerId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeSale.add(labelCustomerId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerId.setPreferredSize(new Dimension(100,25));
        panelMakeSale.add(textCustomerId,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeSale.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeSale.add(labelQuantity, gbc);

        labelProduct = new JLabel("Product 1:", JLabel.RIGHT);
        labelProduct.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeSale.add(labelProduct, gbc);

        textBarcode = new JTextField();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textBarcode.setPreferredSize(new Dimension(100,25));
        panelMakeSale.add(textBarcode,gbc);

        textQuantity = new JTextField();
        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textQuantity.setPreferredSize(new Dimension(100,25));
        panelMakeSale.add(textQuantity,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeSale.add(errorMessage, gbc);

        buttonAdd = new JButton("Add more");
        buttonRemove = new JButton("Remove");
        buttonSubmit = new JButton("Submit");
        buttonBack = new JButton("Back");
        buttonClear = new JButton("Clear");

        buttonAdd.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    if(textBarcode.getText().equals("") || textQuantity.getText().equals("")){
                        errorMessage.setText("Not possible!");
                        buttonAdd.setEnabled(false);
                        buttonRemove.setEnabled(false);
                        buttonSubmit.setEnabled(false);
                        buttonBack.setEnabled(false);
                    }else{

                        if (saleId==0)
                            saleId = saleController.createSale(Integer.parseInt(textCustomerId.getText()), sessionId);
                        String barcode = textBarcode.getText();
                        int productBarcode = Integer.parseInt(barcode);
                        int amount = Integer.parseInt(textQuantity.getText());
                        boolean check = saleController.addProductInSale(saleId,productBarcode,amount);
                        if(!check) {
                            saleController.removeProductFromSale(saleId,productBarcode,amount);
                        }

                        count++;
                        labelProduct = new JLabel("Product " + (count+1) + ":", JLabel.RIGHT);
                        labelProduct.setPreferredSize(new Dimension(100, 25));
                        gbc.gridx = 0;
                        gbc.gridy = 3 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        panelMakeSale.add(labelProduct, gbc);

                        textBarcode = new JTextField();
                        gbc.gridx = 1;
                        gbc.gridy = 3 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        textBarcode.setPreferredSize(new Dimension(100,25));
                        panelMakeSale.add(textBarcode,gbc);

                        textQuantity = new JTextField();
                        gbc.gridx = 2;
                        gbc.gridy = 3 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        textQuantity.setPreferredSize(new Dimension(100,25));
                        panelMakeSale.add(textQuantity,gbc);

                        errorMessage = new JLabel("", JLabel.RIGHT);
                        errorMessage.setPreferredSize(new Dimension(100, 25));
                        gbc.gridx = 0;
                        gbc.gridy = 4 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        panelMakeSale.add(errorMessage, gbc);

                        gbc.gridx = 0;
                        gbc.gridy = 5 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonAdd.setPreferredSize(new Dimension(100,25));
                        panelMakeSale.add(buttonAdd,gbc);

                        gbc.gridx = 1;
                        gbc.gridy = 5 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonRemove.setPreferredSize(new Dimension(100,25));
                        panelMakeSale.add(buttonRemove,gbc);

                        gbc.gridx = 2;
                        gbc.gridy = 5 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonSubmit.setPreferredSize(new Dimension(100,25));
                        panelMakeSale.add(buttonSubmit,gbc);

                        gbc.gridx = 1;
                        gbc.gridy = 6 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonClear.setPreferredSize(new Dimension(100,25));
                        panelMakeSale.add(buttonClear,gbc);

                        gbc.gridx = 2;
                        gbc.gridy = 6 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonBack.setPreferredSize(new Dimension(100,25));
                        panelMakeSale.add(buttonBack,gbc);

                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        buttonAdd.setPreferredSize(new Dimension(100,25));
        panelMakeSale.add(buttonAdd,gbc);

        buttonRemove.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){

                    //  saleController.removeProductFromSale(saleId,Integer.parseInt(textBarcode),Integer.parseInt(textQuantity));

                    container = getContentPane();
                    panelMakeSale.remove(labelProduct);
                    panelMakeSale.remove(textBarcode);
                    panelMakeSale.remove(textQuantity);
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count--;

                    gbc.gridx = 0;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonAdd.setPreferredSize(new Dimension(100,25));
                    panelMakeSale.add(buttonAdd,gbc);

                    gbc.gridx = 1;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonRemove.setPreferredSize(new Dimension(100,25));
                    panelMakeSale.add(buttonRemove,gbc);

                    gbc.gridx = 2;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonSubmit.setPreferredSize(new Dimension(100,25));
                    panelMakeSale.add(buttonSubmit,gbc);

                    gbc.gridx = 1;
                    gbc.gridy = 6 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonClear.setPreferredSize(new Dimension(100,25));
                    panelMakeSale.add(buttonClear,gbc);

                    gbc.gridx = 2;
                    gbc.gridy = 6 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonBack.setPreferredSize(new Dimension(100,25));
                    panelMakeSale.add(buttonBack,gbc);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        buttonRemove.setPreferredSize(new Dimension(100,25));
        panelMakeSale.add(buttonRemove,gbc);

        buttonSubmit.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){

                    if (saleId==0)
                        saleId = saleController.createSale(Integer.parseInt(textCustomerId.getText()), sessionId);
                    int productBarcode = Integer.parseInt(textBarcode.getText());
                    int amount = Integer.parseInt(textQuantity.getText());
                    boolean check = saleController.addProductInSale(saleId,productBarcode,amount);
                    if(!check) {
                        saleController.removeProductFromSale(saleId,productBarcode,amount);
                    }

                    saleController.endSale(saleId, sessionId);
                    saleId=0;
                    count = 0;
                    container = getContentPane();
                    panelMakeSale.removeAll();
                    container.removeAll();
                    menuSales();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        buttonSubmit.setPreferredSize(new Dimension(100,25));
        panelMakeSale.add(buttonSubmit,gbc);

        buttonClear.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textBarcode.setText("");
                    textQuantity.setText("");
                    buttonAdd.setEnabled(true);
                    buttonRemove.setEnabled(true);
                    buttonSubmit.setEnabled(true);
                    buttonBack.setEnabled(true);
                    container = getContentPane();
                    panelMakeSale.removeAll();
                    container.removeAll();
                    makeSale();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count = 0;
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        buttonClear.setPreferredSize(new Dimension(100,25));
        panelMakeSale.add(buttonClear,gbc);

        buttonBack.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelMakeSale.removeAll();
                    container.removeAll();
                    menuSales();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count = 0;
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        buttonBack.setPreferredSize(new Dimension(100,25));
        panelMakeSale.add(buttonBack,gbc);
    }

    public void makeSale(){
        menuMakeSale();
        this.getContentPane().add(panelMakeSale, BorderLayout.NORTH);
    }

    private void menuShowSale(){
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelShowSale.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelShowSale.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelShowSale.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        findSaleSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findSale();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No sale!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelShowSale.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textId.setText("");
                    errorMessage.setText("");

                    container = getContentPane();
                    panelShowSale.removeAll();
                    container.removeAll();
                    menuSales();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelShowSale.add(back,gbc);
    }

    public void findSale()
    {
        menuShowSale();
        this.getContentPane().add(panelShowSale, BorderLayout.NORTH);
    }

    public void findSecondWindow(int id){
        JTextField textId = new JTextField();
        JTextField textDate = new JTextField();
        JTextField textTotal = new JTextField();
        JTextField textEmployee = new JTextField();
        JTextField textCustomer = new JTextField();
        JLabel labelId;
        JLabel labelDate;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JLabel labelNull;
        JLabel labelTotal;
        JLabel labelEmployee;
        JLabel labelCustomer;
        JButton ok;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textDate.setEditable(false);

        textTotal.setEditable(false);
        textEmployee.setEditable(false);
        textCustomer.setEditable(false);

        textId.setText(Integer.toString(saleController.findSale(id).getId()));
        textDate.setText(saleController.findSale(id).getDate());
        textTotal.setText(Double.toString(saleController.findSale(id).getTotalPrice()));
        textEmployee.setText(Integer.toString(saleController.findSale(id).getEmployeeID()));
        textCustomer.setText(Integer.toString(saleController.findSale(id).getCustomerID()));

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelShowSale2.add(labelId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelShowSale2.add(textId,gbc);

        labelDate = new JLabel("Date:", JLabel.RIGHT);
        labelDate.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelShowSale2.add(labelDate, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textDate.setPreferredSize(new Dimension(100,25));
        panelShowSale2.add(textDate,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelShowSale2.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelShowSale2.add(labelQuantity, gbc);

        int y = 4;
        for(int i=0; i<SaleContainer.getInstance().findSaleById(id).getProducts().size(); i++){

            int productNumber = i;
            labelProduct = new JLabel("Product "+ ++productNumber +":", JLabel.RIGHT);
            labelProduct.setPreferredSize(new Dimension(100, 25));
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            panelShowSale2.add(labelProduct, gbc);

            textBarcode = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textBarcode.setPreferredSize(new Dimension(100,25));
            panelShowSale2.add(textBarcode,gbc);

            textQuantity = new JTextField();
            gbc.gridx = 2;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textQuantity.setPreferredSize(new Dimension(100,25));
            panelShowSale2.add(textQuantity,gbc);
            y++;

            textBarcode.setEditable(false);
            textQuantity.setEditable(false);
            int key = (int) SaleContainer.getInstance().findSaleById(id).getProducts().keySet().toArray()[i];
            textBarcode.setText(Integer.toString(key));
            textQuantity.setText(Integer.toString(saleController.findSale(id).getProducts().get(key)));
        }

        labelNull = new JLabel("", JLabel.RIGHT);
        labelNull.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelShowSale2.add(labelNull, gbc);

        labelEmployee = new JLabel("Employee:", JLabel.RIGHT);
        labelEmployee.setPreferredSize(new 

            Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelShowSale2.add(labelEmployee, gbc);

        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textEmployee.setPreferredSize(new 
            Dimension(100,25));
        panelShowSale2.add(textEmployee,gbc);

        labelCustomer = new JLabel("Customer:", JLabel.RIGHT);
        labelCustomer.setPreferredSize(new 

            Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelShowSale2.add(labelCustomer, gbc);

        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textCustomer.setPreferredSize(new 

            Dimension(100,25));
        panelShowSale2.add(textCustomer,gbc);

        labelTotal = new JLabel("Total:", JLabel.RIGHT);
        labelTotal.setPreferredSize(new 

            Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelShowSale2.add(labelTotal, gbc);

        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textTotal.setPreferredSize(new 

            Dimension(100,25));
        panelShowSale2.add(textTotal,gbc);

        ok = new JButton("OK");
        ok.addActionListener(new 

            ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findSale();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        ok.setPreferredSize(new Dimension(100,25));
        panelShowSale2.add(ok,gbc);
    }

    public void findSaleSecondWindow(int id)
    {
        findSecondWindow(id);
        this.getContentPane().add(panelShowSale2, BorderLayout.NORTH);
    }

    private void deleteFirstWindow(){
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteSale.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteSale.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteSale.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        deleteSaleSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        deleteSaleFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No sale!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelDeleteSale.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    menuSales();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelDeleteSale.add(back,gbc);
    }

    private void deleteSaleFirstWindow(){
        deleteFirstWindow();
        this.getContentPane().add(panelDeleteSale, BorderLayout.NORTH);
    }

    private void deleteSecondWindow(int id){
        JTextField textId = new JTextField();
        JTextField textDate = new JTextField();
        JTextField textTotal = new JTextField();
        JTextField textEmployee = new JTextField();
        JTextField textCustomer = new JTextField();
        JLabel labelId;
        JLabel labelDate;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JLabel labelNull;
        JLabel labelTotal;
        JLabel labelEmployee;
        JLabel labelCustomer;
        JButton delete;
        JButton cancel;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textDate.setEditable(false);

        textTotal.setEditable(false);
        textEmployee.setEditable(false);
        textCustomer.setEditable(false);

        textId.setText(Integer.toString(saleController.findSale(id).getId()));
        textDate.setText(saleController.findSale(id).getDate());
        textTotal.setText(Double.toString(saleController.findSale(id).getTotalPrice()));
        textEmployee.setText(Integer.toString(saleController.findSale(id).getEmployeeID()));
        textCustomer.setText(Integer.toString(saleController.findSale(id).getCustomerID()));

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteSale2.add(labelId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteSale2.add(textId,gbc);

        labelDate = new JLabel("Date:", JLabel.RIGHT);
        labelDate.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteSale2.add(labelDate, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textDate.setPreferredSize(new Dimension(100,25));
        panelDeleteSale2.add(textDate,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteSale2.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteSale2.add(labelQuantity, gbc);

        int y = 4;
        for(int i=0; i<SaleContainer.getInstance().findSaleById(id).getProducts().size(); i++){

            int productNumber = i;
            labelProduct = new JLabel("Product "+ ++productNumber +":", JLabel.RIGHT);
            labelProduct.setPreferredSize(new Dimension(100, 25));
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            panelDeleteSale2.add(labelProduct, gbc);

            textBarcode = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textBarcode.setPreferredSize(new Dimension(100,25));
            panelDeleteSale2.add(textBarcode,gbc);

            textQuantity = new JTextField();
            gbc.gridx = 2;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textQuantity.setPreferredSize(new Dimension(100,25));
            panelDeleteSale2.add(textQuantity,gbc);
            y++;

            textBarcode.setEditable(false);
            textQuantity.setEditable(false);
            int key = (int) SaleContainer.getInstance().findSaleById(id).getProducts().keySet().toArray()[i];
            textBarcode.setText(Integer.toString(key));
            textQuantity.setText(Integer.toString(saleController.findSale(id).getProducts().get(key)));
        }

        labelNull = new JLabel("", JLabel.RIGHT);
        labelNull.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteSale2.add(labelNull, gbc);

        labelEmployee = new JLabel("Employee:", JLabel.RIGHT);
        labelEmployee.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteSale2.add(labelEmployee, gbc);

        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textEmployee.setPreferredSize(new Dimension(100,25));
        panelDeleteSale2.add(textEmployee,gbc);

        labelCustomer = new JLabel("Customer:", JLabel.RIGHT);
        labelCustomer.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteSale2.add(labelCustomer, gbc);

        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textCustomer.setPreferredSize(new Dimension(100,25));
        panelDeleteSale2.add(textCustomer,gbc);

        labelTotal = new JLabel("Total:", JLabel.RIGHT);
        labelTotal.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteSale2.add(labelTotal, gbc);

        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textTotal.setPreferredSize(new Dimension(100,25));
        panelDeleteSale2.add(textTotal,gbc);

        delete = new JButton("Delete");
        delete.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    saleController.deleteSale(id);
                    container = getContentPane();
                    container.removeAll();
                    deleteSaleFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        delete.setPreferredSize(new Dimension(100,25));
        panelDeleteSale2.add(delete,gbc);

        cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    deleteSaleFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        cancel.setPreferredSize(new Dimension(100,25));
        panelDeleteSale2.add(cancel, gbc);
    }

    private void deleteSaleSecondWindow(int id){
        deleteSecondWindow(id);
        this.getContentPane().add(panelDeleteSale2, BorderLayout.NORTH);
    }

}
